package com.wjh.api.service;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.wjh.alipay.config.AlipayConfig;
import com.wjh.base.BaseController;
import com.wjh.bean.Constants;
import com.wjh.bean.Result;
import com.wjh.dao.PaymentInfoDao;
import com.wjh.feign.OrderServiceFeign;
import com.wjh.pojo.PaymentInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @Author: 王金海
 * @Description:
 * @Date: Created by Administrator on 2018/6/27.
 * @Modified By:
 */
@RestController
@Slf4j
public class PayCallBackServiceImpl extends BaseController implements PayCallBackService {

    @Autowired
    private PaymentInfoDao paymentInfoDao;

    @Autowired
    private OrderServiceFeign orderServiceFeign;
    @Override
    public Result synCallBack(@RequestParam Map<String, String> params) {
        log.warn("----------------同步回调开始---------------");
        log.info("params:{}",params);
        try {
            //调用SDK验证签名
            boolean signVerified = AlipaySignature.rsaCheckV1(params, AlipayConfig.alipay_public_key, AlipayConfig.charset, AlipayConfig.sign_type);
            if (!signVerified) {
                return error("验签失败");
            }
            //商户订单号
            String out_trade_no = params.get("out_trade_no");
            //支付宝交易号
            String trade_no = params.get("trade_no");
            //付款金额
            String total_amount = params.get("total_amount");
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("out_trade_no",out_trade_no);
            jsonObject.put("trade_no",trade_no);
            jsonObject.put("total_amount",total_amount);
            return success(jsonObject);
        } catch (AlipayApiException e) {
            log.error("验签失败:{}",e);
            return error("验签失败");
        }finally {
            log.warn("----------------同步回调结束---------------");
        }
    }

    @Override
    public String asynCallBack(@RequestParam Map<String, String> params) {
        log.warn("----------------异步回调开始---------------");
        log.info("params:{}",params);
        try {
            //调用SDK验证签名
            boolean signVerified = AlipaySignature.rsaCheckV1(params, AlipayConfig.alipay_public_key, AlipayConfig.charset, AlipayConfig.sign_type);
            if (!signVerified) {
                return Constants.PAY_FAIL;
            }
            //商户订单号
            String out_trade_no = params.get("out_trade_no");
            //支付宝交易号
            String trade_no = params.get("trade_no");
            //付款金额
            String total_amount = params.get("total_amount");
            PaymentInfo byOrderIdPayInfo = paymentInfoDao.getByOrderIdPayInfo(out_trade_no);
            if (ObjectUtils.isEmpty(byOrderIdPayInfo)) {
                return Constants.PAY_FAIL;

            }
            String pic = byOrderIdPayInfo.getPrice() + ".00";
            if (!pic.equals(total_amount)) {
                log.error("订单异常：{}",params);
                //异常订单处理
                return Constants.PAY_FAIL;
            }
            if (byOrderIdPayInfo.getState()==1) {
                return Constants.PAY_SUCCESS;
            }

            PaymentInfo paymentInfo = new PaymentInfo();
            paymentInfo.setState(1);
            paymentInfo.setOrderId(out_trade_no);
            paymentInfo.setPlatformorderId(trade_no);
            paymentInfo.setPayMessage(params.toString());
            // TODO: 2018/6/28 开启事物
            Integer updatePayInfo = paymentInfoDao.updatePayInfo(paymentInfo);
            if (updatePayInfo<=0) {
                return Constants.PAY_FAIL;
            }
            Result result = orderServiceFeign.updateOrderIdInfo(1l, trade_no, out_trade_no);
            if (result.getCode()!= Constants.HTTP_RES_CODE_200) {
                // TODO: 2018/6/28 回滚事物
                return Constants.PAY_FAIL;

            }
            // TODO: 2018/6/28 提交事物
            return Constants.PAY_SUCCESS;
        } catch (AlipayApiException e) {
            // TODO: 2018/6/28 回滚事物
            log.error("验签失败:{}",e);
            return Constants.PAY_FAIL;
        }finally {
            log.warn("----------------异步回调结束---------------");
        }
    }
}
